package com.jxzyk.model;

import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.entity.I_List;
import com.core.ui.activity.BaseActivity;
import com.core.util.T;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.adapter.CourseAdapter;
import com.jxzyk.adapter.KnowAdapter;
import com.jxzyk.config.API;
import com.jxzyk.entity.Course;
import com.jxzyk.entity.QAList;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.Query;
import com.jxzyk.http.req.CourseRequest;
import com.jxzyk.http.req.QARequest;
import com.jxzyk.model.course.CourseActivity;
import com.jxzyk.model.know.KnowActivity;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.act_search)
public class SearchActivity extends BaseActivity {
	@ViewInject(R.id.title)
	TitleLayout title;
	@ViewInject(R.id.list_search)
	private ListView listView;

	private List<I_List> lists = new ArrayList<>();
	private String url;

	@Override
	public void initData(Bundle bundle) {
		bundle = getIntent().getExtras();
		if (bundle != null) {
			url = bundle.getString(SearchActivity.class.getName());
		}
	}

	@Override
	public void initUi() {
		title.setVisibility(2);
		title.setSearchListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (title.isSearch()) {// doNet
					String keywords = UiTools.getString(title.et_search);
					switch (url) {
					case API.Course.getCourses:
						loadCourseList(keywords);
						break;
					case API.QA.list:
						loadQaList(keywords);
						break;
					default:
						break;
					}

				} else {
					finish();
				}
			}

		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Bundle bundle = new Bundle();
				try {
					switch (url) {
					case API.Course.getCourses:
						Course course = (Course) lists.get(arg2);
						bundle.putSerializable(course.getClass().getName(),
								course);
						showActivity(CourseActivity.class, bundle);
						break;
					case API.QA.list:
						QAList qaList = (QAList) lists.get(arg2);
						bundle.putSerializable(qaList.getClass().getName(),
								qaList);
						showActivity(KnowActivity.class, bundle);
						break;
					default:
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
					T.toast("下标越界[listsize]" + lists.size() + "[index]" + arg2);
				}
			}
		});
	}

	private void loadQaList(String keywords) {
		QARequest request = new QARequest(SearchActivity.this, API.QA.list);
		Query query = new Query();
		query.setKeywords(keywords);
		request.setQuery(query);
		lists.clear();
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					JSONArray array = json.getJSONArray("data");
					if (array.size() > 0) {
						for (int i = 0; i < array.size(); i++) {
							QAList qaList = (QAList) new QAList()
									.instance(array.getJSONObject(i));
							lists.add(qaList);
						}
						KnowAdapter adapter = (KnowAdapter) new KnowAdapter()
								.instance(getBaseContext(), lists);
						listView.setAdapter(adapter);
						// adapter.notifyDataSetChanged();
					}
					// Call onRefreshComplete when the list has been refreshed.
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				// TODO Auto-generated method stub

			}
		});
		request.show();
	}

	private void loadCourseList(String keywords) {
		CourseRequest request = new CourseRequest(this, API.Course.getCourses);
		Query query = new Query();
		query.setKeywords(keywords);
		request.setQuery(query);
		lists.clear();
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					JSONArray array = json.getJSONArray("data");
					if (array.size() > 0) {
						for (int i = 0; i < array.size(); i++) {
							Course course = (Course) new Course()
									.instance(array.getJSONObject(i));
							lists.add(course);
						}
						CourseAdapter adapter = (CourseAdapter) new CourseAdapter()
								.instance(getBaseContext(), lists);
						listView.setAdapter(adapter);
						// adapter.notifyDataSetChanged();
					}
					// Call onRefreshComplete when the list has been refreshed.
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				// adapter.notifyDataSetChanged();
			}
		});
		request.show();
	}
}
